package com.lvmama.config;

import java.net.InetAddress;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;


/**
 * @ClassName : ElasticsearchConfig
 * @Description : ES查询
 * @Author :Administrator
 * @Date : 2018/9/6 9:33
 * @Version V1.0
 **/
@Configuration
public class ElasticsearchConfig {
    @Value("${cluster-name}")
    private  String clsuerName;
    @Value("${cluster-nodes}")
    private  String clsuerAddress;
    @Bean
    public  ElasticsearchTemplate elasticsearchTemplate() throws Exception{

        Settings settings = Settings.builder().put("cluster.name", clsuerName).build();
        TransportClient transportClient = TransportClient.builder().settings(settings).build();
        for (String address : clsuerAddress.split(",")){
        	transportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(address), 9300));
        }
        return new ElasticsearchTemplate(transportClient);
    }
    
}
